package cn.kgc.vue.commons;

import cn.kgc.vue.exceptions.StudentNoException;
import cn.kgc.vue.exceptions.TokenTheftException;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 课工场
 * @date 2023/12/4
 * @description  全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(UnknownAccountException.class)
    public ResponseResult unknownAccountExceptionHandler(UnknownAccountException e){
        e.printStackTrace();
        return ResponseResult.error().message("用户名错误");
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult incorrectCredentialsExceptionHandler(IncorrectCredentialsException e){
        e.printStackTrace();
        return ResponseResult.error().message("密码错误");
    }

    @ExceptionHandler(AuthenticationException.class)
    public ResponseResult authenticationExceptionHandler(AuthenticationException e){
        e.printStackTrace();
        return ResponseResult.error().code(ResultCode.NO_AUTHENTICATED.getCode()).message(ResultCode.NO_AUTHENTICATED.getMessage());
    }


    @ExceptionHandler(UnauthorizedException.class)
    public ResponseResult  tokenExpiredExceptionHandler( UnauthorizedException e){
        e.printStackTrace();
        return ResponseResult.error().code(403).message("没有权限,请联系管理员");
    }

    @ExceptionHandler(TokenTheftException.class)
    public ResponseResult  tokenTheftExceptionHandler( TokenTheftException e){
        e.printStackTrace();
        return ResponseResult.error().message("请自己登录后访问!!!");
    }

    @ExceptionHandler({SignatureVerificationException.class, JWTDecodeException.class})
    public ResponseResult  tokenExceptionHandler(Exception e){
        e.printStackTrace();
        return ResponseResult.error().message("token被篡改!!!");
    }

    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult lockedAccountExceptionHandler(LockedAccountException e){
        e.printStackTrace();
        return ResponseResult.error().message("账号被禁用,请联系管理员!!!");
    }



    @ExceptionHandler(StudentNoException.class)
    public ResponseResult otherExceptionHandler(StudentNoException e){
        e.printStackTrace();
        return ResponseResult.error().message("学号有重复");
    }


    @ExceptionHandler(Exception.class)
    public ResponseResult otherExceptionHandler(Exception e){
        e.printStackTrace();
        return ResponseResult.error();
    }

}
